<?php

namespace App\Http\Controllers\Search;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\SearchKeyword;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;

class SearchController extends Controller
{
    protected $ret;

	public function __construct()
    {
    	$this->ret = [];
    }

	//20180925热搜关键词
    public function index(Request $request){
    	$keyword = new SearchKeyword();
    	
    	$where= array();
    	if (Input::has('keyword')){
    		$keyword = Input::get('keyword');
    		if(is_numeric($keyword)){
    			$where['id'] = $keyword;
    		}else{
    			$where['keyword'] = $keyword;
    		}
		}
		if (Input::has('state')){
    		$where['state'] = Input::get('state');
		}

		$list = $keyword->listinfo($where);

    	$this->ret['list'] = $list;
    	$this->ret['state']= Input::get('state');
    	$this->ret['keyword']= Input::get('keyword');
        
        return view('search.index', $this->ret);
    }

    //20180712 添加标签
    public function addKeyword(Request $request){
    	$id = $request->input("id");
    	if($id){
            $keyword = SearchKeyword::where('id',$id)->first();
        }else{
            $keyword = new SearchKeyword();
        }
        $keyword->keyword = $request->input("keyword");
        $keyword->orderby = $request->input("orderby");
        $keyword->state   = $request->input("state");
        $keyword->opertion_user_id = $request->user()->id;
    	if($keyword->save()){
    		return json_encode(array("code"=>1, 'msg'=>'添加成功'));
        }
    }

    public function deleteKeyword(Request $request){
    	$tags = new SearchKeyword();
    	$id   = $request->input("id");
    	$result = $tags->where("id", $id)->delete();
    	if($result){
    		return $this->getMessage(1,'删除成功');
    	}else{
    		return $this->getMessage(0,'删除失败');
    	}
    }
}
